# -*- coding: utf-8 -*-
# @Time    : 2021/12/31
# @Author  : quanjie

import importlib
import logging
import os
import sys
import time

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0,BASE_DIR)
sys.path.insert(0,os.path.join(BASE_DIR,'quanjie_spider'))


logger = logging.getLogger()
logger.setLevel(logging.INFO)


def logFunc(task_name):
    try:
        log_dir = os.path.join(os.path.join(BASE_DIR,'quanjie_spider'),'log')
        if not os.path.exists(log_dir):
            os.mkdir(log_dir)
        fh = logging.FileHandler(log_dir + f"/{task_name}." + str(time.time())[:10],encoding='utf-8')
        fh.setLevel(logging.INFO)
        formatter = logging.Formatter('%(asctime)s - %(filename)s(%(lineno)d) -%(levelname)s - %(message)s')
        fh.setFormatter(formatter)
        logger.addHandler(fh)
    except:
        logging.error("log init error")
        exit("log init error")


def startCrawler(crawler_name, *args, **kwargs):
    module_name = 'crawlers.%s' % crawler_name
    main_class = importlib.import_module(module_name)
    crawler_class = main_class.Crawler()
    crawler_class.execute(*args,**kwargs)


if __name__ == '__main__':
    try:
        # sys.argv=['',"google_search.crawler2"]
        if len(sys.argv) >= 1:
            crawler_name = sys.argv[1]
            kwargs = {}

            logFunc(task_name=crawler_name)  # 初始化日志系统
            args = sys.argv[1:]
            try:
                logger.info(f"*****************************爬虫任务{crawler_name}*****************************")

                startCrawler(crawler_name, *sys.argv, **kwargs)

                logger.info("**********************************数据爬取成功***********************************")
                exit(0)
            except Exception as e:
                logger.exception(e)
                logger.error("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!数据爬取失败!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
                raise Exception(f"crawler_name - {crawler_name} 运行失败")

        else:
            logging.error(f"输入的参数不正确{sys.argv}")
            raise Exception(f"输入的参数不正确{sys.argv}")
    finally:
        pass